<?php
/**
 * User: rongxia
 * Date: 2018/08/07
 * Time: 23:42
 */

namespace App\Services\ThirdAPI;



use common\utils\RedisKeys;
//use App\Models\Order;
use App\Models\Game;
use App\Models\Partner;
use Illuminate\Support\Facades\Log;

class GNSAPIService {
    protected $apiName  = 'GNS';

    /**
     * 代理商获取玩家游戏下注数据或总社获取玩家游戏下注日志   OK
     * @param int $startTime 抓取时间段的开始时间
     * @param int $endTime 抓取时间段的结束时间
     * @return string|unknown
     */
    public function getGNSOrders($startTime, $endTime) {
        Log::info("GNS 获取注单：" . date("Y-m-d H:i:s", $startTime) . " - " . date("Y-m-d H:i:s", $endTime));
        $retValue = [];
        $bet = $this->getBetLog($startTime, $endTime);
        if(is_array($bet)&& count($bet)){
            $retValue = array_merge($retValue, $bet);
        }else{
            Log::info("GNS 暂无可更新数据");
        }
        return $retValue;
    }

    /**
     * 获取Gns游戏名字列表
     * @return array|bool
     */
    public function getGNSGameNames()
    {
        $data = Game::where(['partner_name'=>$this->apiName])->get()->toArray();
        foreach ($data as $game) {
            $games[$game['extend']] = $game['name'];
        }
    
        return $games;
    }

    /**
     * 获取用户投注记录
     * @param string $starttime 开始时间(时间戳)
     * @param string $endtime 开始时间(时间戳)
     * @return
     */
    public function getBetLog($starttime, $endtime, $startIndex=0, $limit=500){
        $params = [
            'api_name' => $this->apiName,
            'start_time' => $starttime +3600*4,
            'end_time' => $endtime +3600*4,
//            'start_time' => 1533572655,//测试用数据
//            'end_time' => 1533659055,//测试用数据
        ];
        $url = config('app.rpcserver') . "/api/getOrders";
        Log::info("GNS 请求接口 url: {$url}, data: " . json_encode($params));
        $response = GameApiService::request($url, 'get', $params);
        $msg="GNS 获取注单抓取接口请求url:====" . $url . "请求参数:======" . json_encode($params, JSON_UNESCAPED_UNICODE) . "请求时间:====" . date("Y-m-d H:i:s") . "总共有" . count($response) . "条数据";
        Log::info($msg);
        $this->amount_format_array_gns($response,['bet_money','prize_money','winloss_money','contribute_money','bonus_money','valid_money']);
        return $response;
    }
    
    /**
     * GNS十倍转换
     * @param array $arr   需转换的金额数组
     * @param array $keys  需转换的键，为空则转换全部键
     * @return array
     */
    public function amount_format_array_gns(&$arr, $keys = [])
    {
        if (empty($arr)) {
            return $arr;
        }
        // 只处理数组
        if (is_array($arr)) {
            foreach ($arr as $k => &$v) {
                if (is_array($v)) {
                    $this->amount_format_array_gns($v, $keys);
                } else {
                    if (empty($keys) || in_array($k, $keys)) {
                        $arr[$k] = $v*10;
                    }
                }
            }
        }
        return $arr;
    }
    /**
     * 获取奖池
     * @return
     */
    public function getBonusPool($game_code){
        $params = [
            'api_name' => $this->apiName,
            'game_code' => $game_code,
        ];
        $url = config('app.rpcserver') . "/api/getBonusPool";
        Log::info("GNS 请求奖池接口 url: {$url}, data: " . json_encode($params));
        $response = GameApiService::request($url, 'get', $params);
        $msg="GNS 获取奖池接口请求url:====" . $url . "请求参数:======" . json_encode($params, JSON_UNESCAPED_UNICODE) . "请求时间:====" . date("Y-m-d H:i:s");
        Log::info($msg);
//        amount_format_array($response,['total_bet','total_won'],true);
        return $response;
    }
    
}